package com.qwc.blog.service.impl;

import com.github.benmanes.caffeine.cache.Cache;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import static com.qwc.blog.common.constant.CommonConstant.CACHE;

/**
 * @author qwc
 * @Date 2021/10/20 16:56
 */
@Service
@Slf4j
@CacheConfig(cacheNames = CACHE)
public class TestService {

    @Autowired
    private Cache<String, Object> caffeineCache;

    @Cacheable(key = "#key")
    public String cacheTest(String key, String value) {
        log.info("加入缓存{}", value);
        return value;
    }

    public void cacheTest2(String key, String value) {
        caffeineCache.put(key, value);
    }

    public String getCache2(String key) {
        return (String) caffeineCache.getIfPresent(key);
    }
}
